// This file is part of OpenMeca, an easy software to do mechanical simulation.
//
// Author(s)    :  - Damien ANDRE  <openmeca@gmail.com>
//
// Copyright (C) 2012 Damien ANDRE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.


#ifndef OpenMeca_Item_Link_Revolute_hpp
#define OpenMeca_Item_Link_Revolute_hpp

#include "OpenMeca/Core/AutoRegister.hpp"
#include "OpenMeca/Core/UserItemCommonProperty.hpp"
#include "OpenMeca/Item/LinkBase.hpp"
#include "OpenMeca/Item/PartLink.hpp"
#include "OpenMeca/Gui/UserItemDialogLinkBaseClassic.hpp"
#include "OpenMeca/Util/Color.hpp"
#include "OpenMeca/Core/ItemCommonProperty.hpp"



namespace OpenMeca
{  
  namespace Item
  {

    class Body;

    template <class T>
    class Link: public LinkBase , public Core::AutoRegister< Link<T> >
    {
    
    public:
      static const std::string GetStrType(); 
      static void Init();

      friend class  Gui::UserItemDialogLinkBaseClassic< Link<T> >;
      typedef Gui::UserItemDialogLinkBaseClassic< Link<T> > Dialog;

      

    public:
      Link<T>();
      ~Link<T>();
    
    private:
      void UpdateIcon();
      Core::SetOfBase<Core::UserItem> GetAssociatedSelectedItem();
      

      friend class boost::serialization::access;
      template<class Archive> void save(Archive& ar, const unsigned int) const;
      template<class Archive> void load(Archive& ar, const unsigned int);
      BOOST_SERIALIZATION_SPLIT_MEMBER()

    private:
      PartLink<T,1>* partBody1_;
      PartLink<T,2>* partBody2_;
    
    };

    template <class T>  
    template<class Archive>
    inline void
    Link<T>::save(Archive& ar, const unsigned int) const
    {
      ar << BOOST_SERIALIZATION_BASE_OBJECT_NVP(LinkBase);
    }


    template <class T>  
    template<class Archive>
    inline void
    Link<T>::load(Archive& ar, const unsigned int)
    {
      delete partBody1_;
      delete partBody2_;
      ar >> BOOST_SERIALIZATION_BASE_OBJECT_NVP(LinkBase);
    }
    

    template <class T>  
    inline const std::string 
    Link<T>::GetStrType()
    {
      return T::GetStrType();
    }

    template <class T> 
    inline void 
    Link<T>::Init()
    {
      Core::Singleton< Core::UserItemCommonProperty< Link<T> > >::Get().CreateAction_All();
    }
     

      
    template <class T> 
    inline
    Link<T>::Link()
      :LinkBase(Link<T>::GetStrType()),
       partBody1_(new PartLink<T,1>(*this)),
       partBody2_(new PartLink<T,2>(*this))
    {
    }

    template <class T> 
    inline
    Link<T>::~Link()
    {
    }

    
    template <class T> 
    inline void 
    Link<T>::UpdateIcon()
    {
      T::DrawIcon(GetIcon(), GetBody1().GetColor().GetQColor(), GetBody2().GetColor().GetQColor());
    }


    template <class T> 
    inline Core::SetOfBase<Core::UserItem> 
    Link<T>::GetAssociatedSelectedItem()
    {
      Core::SetOfBase<Core::UserItem> set;
      set.AddItem(*partBody1_);
      set.AddItem(*partBody2_);
      return set;
    }



  }

}







#endif
